package com.zucc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.zucc.controller.req.TeachRoomAddReq;
import com.zucc.controller.req.TeachRoomModifyReq;
import com.zucc.controller.req.TeachRoomSearchReq;
import com.zucc.controller.resp.TeachRoomResp;
import com.zucc.entity.TeachRoom;
import com.zucc.entity.TeachRoomMapper;
import com.zucc.entity.base.MyMapper;
import com.zucc.exception.SyzxException;
import com.zucc.service.TeachRoomService;
import com.zucc.service.base.MyServiceImpl;
import com.zucc.service.base.PageData;
import com.zucc.util.MyPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author : charspan
 * @Description :
 * @create : 2022-04-07 5:53 下午
 */
@Service
public class TeachRoomServiceImpl extends MyServiceImpl<TeachRoom> implements TeachRoomService {

    @Resource
    private TeachRoomMapper teachRoomMapper;

    @Override
    public MyMapper<TeachRoom> mapper() {
        return teachRoomMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(TeachRoomAddReq req) {
        TeachRoom teachRoom = new TeachRoom();
        BeanUtil.copyProperties(req, teachRoom);
        teachRoom.setDelFlag(0);
        teachRoom.setCreateTime(DateUtil.date());
        teachRoom.setModifyTime(DateUtil.date());
        teachRoom.setApprovalStatus(0);
        super.saveKey(teachRoom);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(TeachRoomModifyReq req) {
        TeachRoom teachRoomExist = super.findById(req.getId());
        if (Objects.isNull(teachRoomExist) || teachRoomExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的教室使用信息");
        }
        BeanUtil.copyProperties(req, teachRoomExist, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        teachRoomExist.setModifyTime(DateUtil.date());
        super.updateSelective(teachRoomExist);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOne(Long id) {
        TeachRoom teachRoomExist = super.findById(id);
        if (Objects.isNull(teachRoomExist) || teachRoomExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的教室使用信息");
        }
        teachRoomExist.setDelFlag(1);
        teachRoomExist.setDeleteTime(DateUtil.date());
        super.updateSelective(teachRoomExist);
    }

    @Override
    public PageData<TeachRoomResp> page(TeachRoomSearchReq req) {
        Example example = new Example(TeachRoom.class);
        example.setOrderByClause("modify_time DESC");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag", 0);

        if (StrUtil.isNotBlank(req.getTime())) {
            criteria.andLike("time", "%" + req.getTime() + "%");
        }
        if (StrUtil.isNotBlank(req.getName())) {
            criteria.andLike("name", "%" + req.getName() + "%");
        }
        if (StrUtil.isNotBlank(req.getClassCode())) {
            criteria.andLike("classCode", "%" + req.getClassCode() + "%");
        }
        if (StrUtil.isNotBlank(req.getClassIndex())) {
            criteria.andLike("classIndex", "%" + req.getClassIndex() + "%");
        }
        if (StrUtil.isNotBlank(req.getTeacher())) {
            criteria.andLike("teacher", "%" + req.getClassIndex() + "%");
        }
        if (StrUtil.isNotBlank(req.getWeekRemark())) {
            criteria.andLike("weekRemark", "%" + req.getWeekRemark() + "%");
        }
        if (StrUtil.isNotBlank(req.getAddress())) {
            criteria.andLike("address", "%" + req.getAddress() + "%");
        }
        if (Objects.nonNull(req.getApprovalStatus())) {
            criteria.andEqualTo("approvalStatus", req.getApprovalStatus());
        }
        PageData<TeachRoom> pageData = super.findByPage(req, example);
        PageData<TeachRoomResp> convert = MyPageUtil.convert(pageData, TeachRoomResp.class);
        return convert;
    }
}
